package com.hjxy.controller;

import com.github.pagehelper.PageInfo;
import com.hjxy.pojo.Class1;
import com.hjxy.pojo.Classroom;
import com.hjxy.pojo.Course;
import com.hjxy.service.ClassroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: fcz
 * @createDate: 2020/9/29
 * @version: 1.0
 */
@Controller
@RequestMapping("/class")
public class ClassroomController {
    @Autowired
    private ClassroomService classroomService;

    @GetMapping("/getview")
    public String getClassroom(){
        //直接返回jsp页面
        return "classroomregisterResult";
    }
    //获取展示信息
    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String, Object> getList(){

        List<Classroom> classroomSet = classroomService.getAll();
        System.out.println(classroomSet);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取用户数据成功");
        result.put("classroomList",classroomSet);
        return result;
    }

    @GetMapping("/add")
    public String add(){
        //直接返回jsp页面
        return "classroom-add";
    }

    @RequestMapping("/insertClassroom")
    @ResponseBody
    public Map<String,Object> addOne(Classroom classroom){
        classroomService.addOne(classroom);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","成功");
        result.put("classroom",classroom);
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> deleteById(Integer classroomId){
        classroomService.deleteById(classroomId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","成功");
        return result;
    }

/*
    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> updateByClassroom(Classroom classroom){
        Map<String, Object> result = new HashMap<>();
        //Classroom  classroom = classroomService.updateByClassroom(classroomId);
        boolean modified = classroomService.updateByClassroom(classroom);
        if(!modified){
            result.put("code",-1);
            result.put("message","编辑失败");
        }else {
            result.put("code",0);
            result.put("message","编辑成功");
        }
        return result;*/
        //        boolean modified;
//        if(classroom.getClassroomId() > 0){
//            modified = classroomService.modifyOne(classroom);
//        }else{
//            modified =classroomService.addOne(classroom);
//        }
//        if(classroom!=null){
//            result.put("code", -1);
//            result.put("message", "编辑用户数据失败");
//        }else{
//            result.put("code", 0);
//            result.put("message", "编辑用户数据成功");
//        }
    }


//    //分页
//    @RequestMapping("/getClassroomByPage")
//    @ResponseBody
//    public Map<String, Object> getCourseByBage(Integer currentPage,Integer pageSize){
//        PageInfo<Classroom> pageinfo =classroomService.getClassroomPage(currentPage,pageSize);
//        System.out.println(pageinfo);
//        Map<String,Object> result=new HashMap<>();
//        result.put("code",1);
//        result.put("message","获取成功");
//        result.put("classroomList",pageinfo.getList());//返回查询出来的分页数据
//        result.put("totalPage",pageinfo.getPages());//返回当前总共有多少页
//        return result;
//    }

   /* @RequestMapping("/getListByPage")
    @ResponseBody
    public Map<String, Object> getListByPage(Integer currentPage,
                                             Integer pageSize){

        PageInfo<Classroom> pageInfo = classroomService.getClassroomsByPage(currentPage,pageSize);
        System.out.println(pageInfo.getList());

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("message", "获取用户数据成功");
        result.put("ClassroomList", pageInfo.getList()); // 返回查询出来的分页数据
        result.put("totalPage", pageInfo.getPages()); //返回当前总共有多少页
        return result;
    }

    @RequestMapping("/getone")
    @ResponseBody
    public Map<String, Object> getOne(Integer classroomId){
        Map<String, Object> result = new HashMap<>();
        Classroom classroom = classroomService.getOneByClassroomId(classroomId);
        if(classroom == null){
            result.put("code", -1);
            result.put("message", "获取用户数据失败");
        }else{
            result.put("code", 0);
            result.put("message", "获取用户数据成功");
            result.put("classroom", classroom);
        }
        return result;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Map<String, Object> edit(Classroom classroom){
        Map<String, Object> result = new HashMap<>();
        boolean modified;
        if(classroom.getClassroomId() > 0){
            modified = classroomService.modifyOne(classroom);
        }else{
            modified =classroomService.addOne(classroom);
        }
        if(!modified){
            result.put("code", -1);
            result.put("message", "编辑用户数据失败");
        }else{
            result.put("code", 0);
            result.put("message", "编辑用户数据成功");
        }
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Classroom classroom){
        Map<String, Object> result = new HashMap<>();

        boolean deleted = classroomService.removeOne(classroom);

        if(!deleted){
            result.put("code", -1);
            result.put("message", "删除用户失败");
        }else{
            result.put("code", 0);
            result.put("message", "删除用户成功");
        }
        return result;
    }

        */
